#pragma once
#include<iostream>
#include"vector.h"
#include<assert.h>
using namespace std;
namespace mypriority_queue
{
	template<class T,class container=myvector::vector<T>,class compare=less<T>>
	class priority_queue
	{
		public:
		priority_queue()
		{}
		 
		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_con.push_back(*first);
				first++;
			}
			
			for (int i = (_con.size() - 2) / 2; i >= 0; i--)
			{
				AdJustDown(i);
			}
		}

		void swap(T& t1, T& t2)
		{
			T tmp = t1;
			t1 = t2;
			t2 = tmp;
		}

		void AdJustDown(size_t parent)
		{
			compare comp;
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && comp(_con[child],_con[child + 1]))
					child++;
				if (comp(_con[parent],_con[child]))
					swap(_con[parent], _con[child]);
				else
					return;
				parent = child;
				child = child * 2 + 1;
			}
		}

		void AdJustUp(size_t child)
		{
			compare comp;
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				if (comp(_con[parent],_con[child]))
					swap(_con[parent], _con[child]);
				else return;
				child = parent;
				parent = (parent - 1) / 2;
			}
		}
		
		void push(const T& x)
		{
			_con.push_back(x);
			AdJustUp(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdJustDown(0);
		}

		const T& top()
		{
			assert(!_con.empty());
			return _con[0];
		}

		size_t size() const
		{
			return _con.size();
		}

		bool empty() const
		{
			return _con.empty();
		}

	private:
		container _con;
	};
}
